package com.mycjj.android.activity;

import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class AddressInfoActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_info);
		initHeader();
		init();
	}
	
	private void initHeader() {
		ImageView mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		Button mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		TextView mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("邮寄信息");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AddressInfoActivity.this.finish();
			}
		});
	}
	
	private EditText et_username;
	private EditText et_tel;
	private EditText et_address;
	private EditText et_zipcode;
	private Button btn_submit;
	private void init(){
		goods_id = getIntent().getStringExtra("goods_id");
		needPoints = getIntent().getIntExtra("needPoints", 0);
		btn_submit = (Button)findViewById(R.id.btn_submit);
		et_zipcode = (EditText)findViewById(R.id.et_zipcode);
		et_address = (EditText)findViewById(R.id.et_address);
		et_username = (EditText)findViewById(R.id.et_username);
		et_tel = (EditText)findViewById(R.id.et_tel);
		btn_submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String username = et_username.getText().toString();
				if(TextUtils.isEmpty(username)){
					Toast.makeText(AddressInfoActivity.this, "收货人姓名不能为空!", Toast.LENGTH_LONG).show();
					return;
				}
				
				String tel = et_tel.getText().toString();
				if(TextUtils.isEmpty(tel)){
					Toast.makeText(AddressInfoActivity.this, "收货人电话不能为空!", Toast.LENGTH_LONG).show();
					return;
				}
				
				String address = et_address.getText().toString();
				if(TextUtils.isEmpty(address)){
					Toast.makeText(AddressInfoActivity.this, "收货人地址不能为空!", Toast.LENGTH_LONG).show();
					return;
				}
				
//				String zipCode = et_zipcode.getText().toString();
//				if(TextUtils.isEmpty(zipCode)){
//					Toast.makeText(AddressInfoActivity.this, "邮政编码不能为空!", Toast.LENGTH_LONG).show();
//					return;
//				}
				
				submit(address,username,tel,"");
			}
		});
		
		initData();
	}
	private String goods_id;
	private int needPoints;
	private void submit(String address,String username,String tel,String zipCode){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("goods_id", goods_id);
		params.put("address", address);
		params.put("name", username);
		params.put("mobile", tel);
//		if(!TextUtils.isEmpty(zipCode)){
//			params.put("zipcode", zipCode);
//		}
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String token = Preferences.getSharedPreferences(this, "user_token", "");
		params.put("token", token);
		request.requestSyceeDoCashWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						int point = dataManager.getPoints() - needPoints;
						dataManager.setPoints(point);
						Toast.makeText(AddressInfoActivity.this, "兑换成功!", Toast.LENGTH_SHORT).show();
						AddressInfoActivity.this.finish();
					}else{
						Toast.makeText(AddressInfoActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Log.e("mark", "baseBean is null!");
				}
			}
		});
	}
	
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String token = Preferences.getSharedPreferences(this, "user_token", "");
		params.put("token", token);
		request.requestGetAddressDoneHandler("", params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						Map data_obj = baseBean.getData();
						if(data_obj == null){
							return;
						}
						Object area_obj = data_obj.get("area_info");
						String area_info = "";
						if(area_obj != null){
							area_info = area_obj.toString();
						}
						Object address_obj = data_obj.get("address");
						String address = "";
						if(address_obj != null){
							address = address_obj.toString();
						}
						Object phone_obj = data_obj.get("mob_phone");
						String phone = "";
						if(phone_obj != null){
							phone = phone_obj.toString();
						}
						
						Object true_name_obj = data_obj.get("true_name");
						String true_name = "";
						if(phone_obj != null){
							true_name = true_name_obj.toString();
						}
						Log.e("mark", "area_info:"+area_info+";address:"+address+";phone:"+phone+";true_name:"+true_name);
						et_username.setText(true_name);
						et_tel.setText(phone);
						et_address.setText(area_info+address);
					}else{
						Toast.makeText(AddressInfoActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Log.e("mark", "baseBean is null!");
				}
			}
		});
	}
}
